﻿using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using EpiInfoCloudMagpiBridge.Filters;
using EpiInfoCloudMagpiBridge.Models;
using Ewav.Security;

namespace EpiInfoCloudMagpiBridge.Controllers
{
    [Authorize]
    public class AzureMobileServiceSettingsController : Controller
    {
        readonly Cryptography cy = new Cryptography();
        readonly MagpiEWAVEntities db;

        public AzureMobileServiceSettingsController()
        {
            db = new MagpiEWAVEntities(cy.Decrypt(ConfigurationManager.ConnectionStrings["MagpiEWAVEntities"].ToString()));
        }



        // GET: AzureMobileServiceSettings
        public ActionResult Index()
        {


            return View(db.AzureMobileServiceSettings.ToList());

        }

        // GET: AzureMobileServiceSettings/Details/5
           [SessionExpire]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AzureMobileServiceSetting azureMobileServiceSetting = db.AzureMobileServiceSettings.Find(id);
            if (azureMobileServiceSetting == null)
            {
                return HttpNotFound();
            }
            return View(azureMobileServiceSetting);
        }

        // GET: AzureMobileServiceSettings/Create
           [SessionExpire]
        public ActionResult Create()
        {
            return View();
        }

        // POST: AzureMobileServiceSettings/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [SessionExpire]  
        public ActionResult Create([Bind(Include = "Id,EWAVUserID,MobileServiceName,ApplicationKey")] AzureMobileServiceSetting azureMobileServiceSetting)
        {
            if (ModelState.IsValid)
            {
                azureMobileServiceSetting.EWAVUserID = ApplicationModel.CurrentEwavUserId;
                db.AzureMobileServiceSettings.Add(azureMobileServiceSetting);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(azureMobileServiceSetting);
        }

        // GET: AzureMobileServiceSettings/Edit/5
        [SessionExpire]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AzureMobileServiceSetting azureMobileServiceSetting = db.AzureMobileServiceSettings.Find(id);
            if (azureMobileServiceSetting == null)
            {
                return HttpNotFound();
            }
            return View(azureMobileServiceSetting);
        }

        // POST: AzureMobileServiceSettings/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [SessionExpire]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,EWAVUserID,MobileServiceName,ApplicationKey")] AzureMobileServiceSetting azureMobileServiceSetting)
        {
            if (ModelState.IsValid)
            {
                azureMobileServiceSetting.EWAVUserID = ApplicationModel.CurrentEwavUserId;
                db.Entry(azureMobileServiceSetting).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(azureMobileServiceSetting);
        }

        // GET: AzureMobileServiceSettings/Delete/5
        [SessionExpire]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AzureMobileServiceSetting azureMobileServiceSetting = db.AzureMobileServiceSettings.Find(id);
            if (azureMobileServiceSetting == null)
            {
                return HttpNotFound();
            }
            return View(azureMobileServiceSetting);
        }

        // POST: AzureMobileServiceSettings/Delete/5
        [HttpPost, ActionName("Delete")]
        [SessionExpire]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            AzureMobileServiceSetting azureMobileServiceSetting = db.AzureMobileServiceSettings.Find(id);
            db.AzureMobileServiceSettings.Remove(azureMobileServiceSetting);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
